package com.cms.bs.app.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.SqlRow;
import com.cms.bs.app.service.IAppService;
import com.cms.bs.config.rs.RS;
import com.cms.bs.util.BaseTypeUtil;
import com.cms.bs.util.FreemarkerUtil;
import com.cms.bs.util.JsonUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jiang on 2016-09-28.
 */
@Service
public class AppService implements IAppService {

    private final String CSMPATH = "/com/cms/bs/app";

    @Override
    public RS gen(String json) {
        JSONObject obj = JsonUtil.toJSONObject(json);
        String name = obj.getString("name");
        String t_name = obj.getString("t_name");

        String sysPath = System.getProperty("webapp.root").replace("webapp", "java") + CSMPATH;
        boolean build = FreemarkerUtil.buildConfiguration("gen");
        if(build){
            Map<String, Object> data = new HashMap<>();
            data.put("name", name);
            data.put("t_name", t_name);
            data.put("v_name", StringUtils.uncapitalize(name));

            // 1. Controller
            String filePath = sysPath + "/controller/";
            String fileName = name + "Controller.java";
            FreemarkerUtil.process(data, "controller.ftl", filePath, fileName);

            // 2. Service
            filePath = sysPath + "/service/";
            fileName = name + "Service.java";
            FreemarkerUtil.process(data, "service.ftl", filePath, fileName);

            // 3. Model
            List<SqlRow> srs = Ebean.createSqlQuery("select * from v_info_schema where table_name = '" + t_name + "' and table_schema = 'cms' ").findList();
            if(srs.size()>0){
                processModelData(data, srs);
                filePath = sysPath + "/model/";
                fileName = name + ".java";
                FreemarkerUtil.process(data, "model.ftl", filePath, fileName);
            }
        }
        return RS.ok();
    }

    private void processModelData(Map<String, Object> data, List<SqlRow> srs) {
        List<Map<String, Object>> columns = new ArrayList<>();
        List<Map<String, Object>> getters = new ArrayList<>();
        List<Map<String, Object>> setters = new ArrayList<>();
        for(SqlRow sr : srs){
            String column_name = sr.getString("column_name");
            String data_type = sr.getString("data_type");
            String column_key = sr.getString("column_key");
            String column_comment = sr.getString("column_comment");

            // 1. Column
            Map<String, Object> column = new HashMap<>();
            column.put("name", column_name);
            column.put("type", BaseTypeUtil.type4db2java(data_type));
            if("PRI".equals(column_key)){
                column.put("annotation", "@Id");
            }
            column.put("comment", column_comment);
            columns.add(column);

            // 2. Getter
            Map<String, Object> getter = new HashMap<>();
            getter.put("type", BaseTypeUtil.type4db2java(data_type));
            getter.put("method", StringUtils.capitalize(column_name));
            getter.put("column", column_name);
            getters.add(getter);

            // 3. Setter
            Map<String, Object> setter = new HashMap<>();
            setter.put("type", BaseTypeUtil.type4db2java(data_type));
            setter.put("method", StringUtils.capitalize(column_name));
            setter.put("column", column_name);
            setters.add(setter);
        }
        data.put("columns", columns);
        data.put("getters", getters);
        data.put("setters", setters);
    }

}
